package com.common.job;

import java.util.ArrayList;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.common.util.HttpUtil;
import com.demo.common.model.Stock;
import com.demo.common.model.Stockinfo;
import com.demo.common.model._MappingKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

public class GetStockInfoJob implements Job {

	@Override
	public void execute(JobExecutionContext job) throws JobExecutionException {
		List<Stockinfo> stockinfoList = new ArrayList<Stockinfo>();
		List<Stock> stockList = Stock.dao.findAll();
		String[] stockcodes = new String[10];
		for (int i = 0; i < stockList.size(); i++) {
			stockcodes[i%10] = stockList.get(i).getStockCode();
			if(i > 0 && i % 10 == 0) {
				HttpUtil.getStockInfoList(stockcodes, stockinfoList);
			}
		}
		Stockinfo.dao.batchSave(stockinfoList);
	}
	
	public static void main(String[] args) {
		System.out.println("-----------开始任务");
		DruidPlugin dp = new DruidPlugin("jdbc:mysql://127.0.0.1/jfinal_stock?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull", "root", "root");
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		_MappingKit.mapping(arp);
		// 与web环境唯一的不同是要手动调用一次相关插件的start()方法
		dp.start();
		arp.start();
		try {
			new GetStockInfoJob().execute(null);
		} catch (JobExecutionException e) {
			e.printStackTrace();
		}
		System.out.println("-----------结束任务");
	}

}
